﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;
using System.Drawing.Imaging;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using CERT.PageLibrary.Base;

namespace CERT.AcceptanceTests
{
    [Binding]
    public class AddTeamSteps : TestFixtureBase
    {
        // For additional details on SpecFlow step definitions see http://go.specflow.org/doc-stepdef

        private int TeamCount { get; set; }

        [Given("I am prompted to login")]
        public void GivenIAmPromptedToLogin()
        {
            var info = ScenarioContext.Current.ScenarioInfo;
           // Assert.AreEqual("Logging in with valid credentials", info.Title);
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/");

            Assert.AreEqual(2, CurrentDriver.FindElementsByName("UserName").Count);
        }
        [When(@"I fill in the login page")]
        public void WhenIFillInTheLoginPage(Table table)
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            foreach (var row in table.Rows)
            {
                var textField = CurrentDriver.FindElementByName(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.SendKeys(row["value"]);
            }
       
            var loginButton = CurrentDriver.FindElementByName("Login");

            if (loginButton == null)
                Assert.Fail("Expected to find a button with the value of 'Login'.");

            this.WaitUpTo(500, "login");
            loginButton.Click();
            this.WaitUpTo(500, "login");
           
            var navBar = CurrentDriver.FindElementById("main-content");
            if (navBar == null)
                Assert.Fail("Expected to find the navbar");
            var add2 = CurrentDriver.FindElementsById("team");
            var add3 = CurrentDriver.FindElementsByLinkText("Teams");

        }

        [When("I select the team page")]
        public void WhenISelectTheTeamPage(Table table2)
        {
           
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];

            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/#/Teams");
          
            this.WaitUpTo(500, "team Add failed");

            //TEAM COUNTS
            var teams = CurrentDriver.FindElementsById("team");
            if (teams == null)
                this.TeamCount = 0;
            else
            this.TeamCount = teams.Count;

            this.WaitUpTo(500, "team Add failed");

            var add2 = CurrentDriver.FindElementsById("AddTeamDiv");
           this.WaitUpTo(50, "team Add failed");
                      var Add = CurrentDriver.FindElementById("AddTeam");

            if (Add == null)
                Assert.Fail("Expected to find a Add Team .");
            else
                Add.Click();

            foreach (var row in table2.Rows)
            {
                var textField = CurrentDriver.FindElementById(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.SendKeys(row["value"]);
            }

            var AddButton = CurrentDriver.FindElementById("AddNewButton");

            if (Add == null)
                Assert.Fail("Expected to find a Add Team Save Button.");
            else
                AddButton.Click();


        }

       
        [Then("the result should be another team")]
        public void ThenTheResultShouldBeAnotherTeam()
        {

            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];

            var teams = CurrentDriver.FindElementsById("team");
            var newcount = teams.Count;

            Assert.Greater(newcount,TeamCount);
        }
    }
}



